package queenProblem;

import gui.Gui;

import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * provides functionality to interact with QueenProblemSolver
 * 
 * @author Kevin Munk, Jakob Karolus
 * @version 1.0
 * 
 */
public class Main implements PropertyChangeListener {

	QueenProblemSolver solver;
	Gui gui;

	/**
	 * Erstellt das Spielfeld usw.
	 * 
	 * @param size
	 *            - int
	 * @param delay
	 *            - int
	 */
	public void createQueenProblemSolver(int size, int delay) {
		boolean[][] field = new boolean[size][size];
		solver = new QueenProblemSolver(field, delay);
		solver.addPropertyChangeListener(this);
	}

	public void setGui(Gui gui) {
		this.gui = gui;
	}

	/**
	 * initializes the solver, solves the problem and prints a solution
	 * 
	 * @param args
	 *            unused
	 */
	public void setQueen(ActionEvent evt, int size) {
		int i = Integer.parseInt(evt.getActionCommand());
		boolean[][] field = solver.getPlayField();
		int x = i / size;
		int y = i % size;
		if (field[x][y])
			field[x][y] = false;
		else
			field[x][y] = true;
		solver.getPlayFieldO().setPlayField(field);
	}

	public void solveField() {
		solver.solve();
	}


	public void propertyChange(PropertyChangeEvent evt) {
		if (evt.getPropertyName() == "setQueen") {
			int[] kor = (int[]) evt.getNewValue();
			int i = (solver.getPlayFieldSize() * kor[0]) + kor[1];
			gui.setQueenAuto(i);
		}
	}

	public static void main(String[] args) {
		//System.out.println("test");
		Main controller = new Main();
		Gui application = new Gui(controller);
		controller.setGui(application);

	}

}
